var chatLog = {}, jStoreReady = false, chatData = {};
  
//添加数据
chatLog.addData = function(json, group, time, name, data){
  
  var now = new Date(time);
  var t = now.getFullYear()+''+(now.getMonth()+1)+''+now.getDate();
  if(typeof json[group] == 'undefined'){
    json[group] = {};
  }
  if(typeof json[group][t] == 'undefined'){
    json[group][t] = {};
  }
  var keys = time+'-|||-'+name;
  json[group][t][keys] = data;
  return json;
}

chatLog.getData = function(json, group){ 
  if(typeof json[group] == 'undefined'){
    return "";
  } 
  return json[group];
}

chatLog.getJsonLength = function(json, limit){
  var length = 0;
  for(var i in json){
    for(var j in json[i]){
      length++;
    }
  }
  var countPage = Math.ceil(length/limit);
  return {'countPage':countPage, 'length':length};
}

//获取聊天记录
chatLog.showJsonList = function(json, start, limit){
  var len = 0;
  var logList = {};
  for(var i in json){
    for(var j in json[i]){
      if(len > start && len < start+limit){
        logList[j] = json[i][j];
      }
      if(len > start+limit){
        break;
      }
      len++;
    }
  }
  return logList;
}

function LocalWrite(name, value){
  if(jStoreReady){
    $.jStore.set(name, $.toJSON(value));	
    return true;					  
  }
}

function LocalRead(name, fn){
  if(jStoreReady){    
    data = $.jStore.get(name);
    data = (null == data || ''==data ) ? {} : data;
    fn( data );
    return true;					  
  }
}

function addChatLog(group, time, name, data){
  LocalRead('_chatlog', function(_chatData){
    chatData = chatLog.addData(_chatData, group, time, name, data);
    LocalWrite('_chatlog', chatData);		
  });
}
function getChatLog(group){
  var str = "";
  LocalRead('_chatlog', function(_chatData){ 
    str = chatLog.getData(_chatData, group);
  });
  return str;
}

function htmlEnCode(str)
{
  var s = "";  
  if(str.length == 0) return "";  
  
  s = str.replace(/&/g, "&gt;");  
  s = s.replace(/</g, "&lt;");  
  s = s.replace(/>/g, "&gt;");  
  s = s.replace(/    /g, "&nbsp;"); 
  s = s.replace(/(http:\/\/[^\s]+)/g, function($,$1){  
   return "<a target='_blank' href='"+$1+"'+>"+$1+"</a>";  
  }); 
  s = s.replace(/\'/g, "'");  
  s = s.replace(/\"/g, "'");  
  s = s.replace(/\n/g, " <br>");  
   
  return s;  
}  


function showLog(group, limit) {
  if($('#log-content').css('display') == 'none') {
    $('#log-content').css('display', 'block');
    parent.document.getElementById('chatFrame').width = (parseInt)(parent.document.getElementById('chatFrame').width)+200;
    $('#right-list').hide();
    showLocalLog(group, limit, 0);
    
  }else{
    $('#log-content').css('display', 'none');
    parent.document.getElementById('chatFrame').width = (parseInt)(parent.document.getElementById('chatFrame').width)-200;
    $('#right-list').show();
    $('#chatLog').html('');
  }
  
}

function pageShow(group, oPage, list, p, limit, fn){
  log2(list);
  var pre = '', suf = '';
  if(oPage.countPage == 1){
    pre = '<a><<</a> 　<a><</a> 　';
    suf = ' 　<a>></a> 　<a>>></a>';
  }else if(oPage.countPage == p) {
    pre = '<a href="javascript:;" onclick="'+fn+'('+group+', '+limit+', 1)"><<</a> 　<a href="javascript:;" onclick="'+fn+'('+group+', '+limit+', '+(p-1)+')"><</a> 　';
    suf = ' 　<a>></a> 　<a>>></a>';
  }else if(p == 1) {
    pre = '<a><<</a> 　<a><</a> 　';
    suf = ' 　<a href="javascript:;" onclick="'+fn+'('+group+', '+limit+', '+(p+1)+')">></a> 　<a  href="javascript:;" onclick="'+fn+'('+group+', '+limit+', '+oPage.countPage+')">>></a>';
  }else{
    pre = '<a href="javascript:;" onclick="'+fn+'('+group+', '+limit+', 1)"><<</a> 　<a href="javascript:;" onclick="'+fn+'('+group+', '+limit+', '+(p-1)+')"><</a> 　';
    suf = ' 　<a href="javascript:;" onclick="'+fn+'('+group+', '+limit+', '+(p+1)+')">></a> 　<a  href="javascript:;" onclick="'+fn+'('+group+', '+limit+', '+oPage.countPage+')">>></a>';
  }
  $('#chatPage').html(pre+'<input class="w40 h20" value="'+p+'" /> / '+oPage.countPage+suf);
}

function showLocalLog(group, limit, p){
  var log = getChatLog(group);
  var oPage = chatLog.getJsonLength(log, limit);
  //默认显示最后一页
  if(p == 0){
    p = oPage.countPage;
  }
  //最后一页的条数,显示
  var showList = (p-1) * limit;
  var list = chatLog.showJsonList(log, showList, limit);
  pageShow(group, oPage, list, p, limit, 'showLocalLog');
}


function parseLog(_json) {
  if(typeof _json == 'string') {
    $('#chatLog').html(_json);
  }else{
    for(var i in _json){
      alert(i);
    }
  }
}

function log(data, data2, c) {
  document.getElementById('chatbox').innerHTML += '<font color="'+c+'"><div>'+data+'</div><div class="p5">'+data2+'</font><br />';
  document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
}
  
function log2(list, type) {
  $('#chatLog').html('');
  var t = '';
  for(var i in list){
    var info = i.split('-|||-');
    if(t != timeFormat(info[0], 'y-m-d')){
      t = timeFormat(info[0], 'y-m-d');
      document.getElementById('chatLog').innerHTML += '<font color="blue f14"><div>日期：'+timeFormat(info[0], 'y-m-d')+'</div><br /><hr /><br />';
    }
    document.getElementById('chatLog').innerHTML += '<font color="blue"><div>'+info[1]+'　'+timeFormat(info[0])+'</div><div class="p5">'+list[i]+'</div></font><br />';
  }
}
  
function timeFormat(time, type) {
  if(typeof time != 'undefined'){
    var nowTime = new Date(parseInt(time));
  }else{
    var nowTime = new Date();
  }
  if(type == 'y-m-d'){
    return nowTime.getFullYear()+'-'+(nowTime.getMonth()+1)+'-'+nowTime.getDate();
  }
  return nowTime.getFullYear()+'-'+(nowTime.getMonth()+1)+'-'+nowTime.getDate()+' '+nowTime.getHours()+':'+nowTime.getMinutes()+':'+nowTime.getSeconds();
}

$(function(){     
  try{
    $.jStore.load();
    $.jStore.ready(function(engine){
      $.jStore.flashReady(function(){       
        jStoreReady = true;
      });
    });
  }catch(e){
    alert(e.toSource());
  }
  
});

